# _*_ coding : utf-8 _*_
# @Time : 2023/8/26 11:55
# @Author : 寻梦
# @File : 09_豆瓣电影内容自定义下载
# @Project : code
import urllib.parse
import urllib.request
base_url="https://movie.douban.com/j/chart/top_list?"

headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/30",
}
def create_request(page):
    limit=20
    param = {
        "type": 5,
        "interval_id": "100:90",
        "start": (page-1)*limit,
        "limit": limit
    }
    url=base_url+urllib.parse.urlencode(param)
    rq=urllib.request.Request(url=url,headers=headers)
    return rq
def get_content(request):
    response=urllib.request.urlopen(request)
    content = response.read().decode("utf-8")
    return content
def download_file(content,flag):
    with open('data/douban_'+str(flag)+".json",'w',encoding="utf-8") as fp:
        fp.write(content)

if __name__=="__main__":
    startPage=int(input("请输入起始页面："))
    endPage=int(input("请输入结束页面："))
    for page in range(startPage,endPage+1):
        self_request=create_request(page)
        content=get_content(self_request)
        download_file(content,page)